﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace Billing
{
    public class FormRegister
    {
        private static SortedList<string, Form> activFormList = new SortedList<string, Form>();

        public static void RegisterForm(Form form, string Name)
        {
            if (activFormList.ContainsKey(Name))
                throw new Exception("Form alredy opened");
            activFormList.Add(Name, form);
        }
        public static void UnregisterForm(string Name)
        {
            activFormList.Remove(Name);
        }

        public static Form getForm(string Name)
        {
            if (activFormList.ContainsKey(Name))
                return activFormList[Name];
            else
                return null;
        }
        public static IList<Form> getForms()
        {
            return activFormList.Values;
        }

        public static SortedList<string, Form> getActiveFormList()
        {
            return activFormList;
        }
        public static void Clear()
        {
            activFormList.Clear();
        }

        public static Form getFormInstance(string asmb, string Name)
        {
            if (asmb.Length == 0 || Name.Length == 0)
                throw new System.ArgumentNullException("the parameter asmb or name don't be null");
            try
            {
                Form frm;
                frm = (Form)Activator.CreateInstance(asmb, Name).Unwrap();
                return frm;
            }
            catch (Exception ex)
            { throw ex; }
        }


        public static Form Show(String asmb, String name, bool Instance, Form Parent, Guid elGUID)
        {
            if (asmb.Length == 0 || name.Length == 0)
                throw new System.ArgumentNullException("the parameter asmb or name don't be null");
            bool mIsModal = false;
            try
            {
                Form frm;
                if (Instance || getForm(name) == null)
                {
                    object obj = Activator.CreateInstance(asmb, name).Unwrap();
                    frm = (Form)obj;

                    frm.FormClosed += new FormClosedEventHandler(FormManager.frm_FormClosed);
                    frm.FormClosing += new FormClosingEventHandler(FormManager.frm_FormClosing);
                    frm.Load += new EventHandler(FormManager.frm_FormLoad);
                    if (((IMenuItem)obj).IsMDIChilde && Parent != null)
                        frm.MdiParent = Parent;

                    mIsModal = ((IMenuItem)obj).IsModal;

                }
                else
                {
                    frm = getForm(name);
                }
                if (mIsModal)
                    frm.ShowDialog();
                else
                    frm.Show();
                frm.Activate();
                return frm;

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


        public static void OpenMenuElement(string ModuleName, string TypeName, Form Parent, Guid elGUID)
        {
            try
            {
                Show(System.IO.Path.GetFileNameWithoutExtension(ModuleName), TypeName, false, Parent, elGUID);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }


    }
}
